<template>
	<view class="address">
		<u-navbar :background="navObj" :is-back="false" :is-fixed="true" height="75" width="100%">
			<view class="left-box" @tap="onClickLeft" style="padding-left: 41rpx">
				<u-icon name="arrow-left" size="40" color="black"></u-icon>
			</view>

			<div class="title" style="
          padding-left: 230rpx;
          font-size: 36rpx;
          font-weight: 400;
          color: black;
        ">
				收货地址
			</div>
		</u-navbar>
		<u-empty class="empty" v-if="addressList == '[]'" text="暂无收货地址" mode="address"></u-empty>
		
		<view class="list" v-else>
			<view class="item c-content" v-for="(item, index) in addressList" :key="index">
				<view class="basic" @click="selectAddressData(item)">
					
					<text>{{ item.name }}</text>
					<text>{{ item.mobile }}</text>
					<text class="default" v-show="item.defaultStatus == 1">默认</text>
					<view>
						<div class="region">
							<span>{{item.province}}</span>
							<span>{{item.city}}</span>
							<span>{{item.region}}</span>
							<span>{{ item.detailAddress }}</span>
						</div>
					</view>
				</view>
				<view class="edit">

					<view class="relative">
						<view class="alifont icon-bianji-copy"></view>
						<text class="mr-40" @click="addAddress(item)">编辑</text>
						<view class="alifont icon-lajitong"></view>
						<text @click="removeAddress(item.id)" class="mr-40">删除</text>
						<text @click="setDefaultAddress(item.id)">设为默认地址</text>
					</view>
				</view>
			</view>
			<view style="height: 100px; background: #FFF;"></view>
		</view>
		<view class="outview">
			<button type="default" class="btn" @click="addAddress('')">
				<u-icon name="plus-circle"></u-icon>
				添加新收货人
			</button>
		</view>
		<u-action-sheet :list="removeList" :tips="tips" v-model="showAction" @click="deleteAddressMessage">
		</u-action-sheet>
	</view>
</template>

<script>
	import * as API_Trade from "@/api/trade";
	import * as API_Address from "@/api/address.js";
	import storage from "../../../utils/storage";
	export default {
		data() {
			return {
				navObj: {
					backgroundColor: "white",
				},
				addressList: [], //地址列表
				showAction: false, //是否显示下栏框
				removeList: [{
					text: "确定",
				}, ],
				tips: {
					text: "确定要删除该收货人信息吗？",
				},
				removeId: "", //删除的地址id
				routerVal: "",

			};
		},
		onLoad: function(val) {
			this.routerVal = val;
		},
		onShow() {
			this.addressList = []
			this.getAddressList();
		},
		onHide() {},
		methods: {
			onClickLeft() {
				uni.navigateBack();
			},
			async selectAddressData(val) {
				// await API_Trade.setAddressId(val.id, this.routerVal.way);

				uni.setStorageSync("currentAdress", val);
				uni.navigateBack({
					delta: 1,
				});
			},
			//获取地址列表
			getAddressList() {
				uni.showLoading();

				API_Address.getAddressList({
					id: storage.getUserInfo().id
				}).then((res) => {
					this.addressList = res.data;
					if (this.$store.state.isShowToast) {
						uni.hideLoading();
					}
				});
			},
			//删除地址
			removeAddress(id) {
				this.removeId = id;
				this.showAction = true;
			},
			deleteAddressMessage() {
				API_Address.deleteAddress({
					id: this.removeId
				}).then((res) => {
					if (res.statusCode == 200) {
						uni.showToast({
							icon: "none",
							title: "删除成功",
						});
						this.getAddressList();
					}
				});
			},
			//新建。编辑地址
			addAddress(item) {
				if (item) {
					uni.navigateTo({
						url: "/pages/mine/address/add?info=" + encodeURIComponent(JSON.stringify(item)),
					});
				} else {
					uni.navigateTo({
						url: "/pages/mine/address/add",
					});
				}
			},


			setDefaultAddress(id) {
				uni.showLoading();
				API_Address.setDefaultAddress({
					addressId: id,
					userId: storage.getUserInfo().id
				}).then(res => {
					if (res.data == '000000') {
						uni.showToast({
							icon: "none",
							title: "设置成功",
						});
						uni.hideLoading();
						this.getAddressList();
					}
				})
			}
		},
	};
</script>

<style lang="scss" scoped>
	@import "./address.scss";
</style>
